package LinkedList;

public class MyLinkedLIst implements Ilink {
    static class LinkNode{
        public int val;
        public LinkNode next;
        public LinkNode prev;

        public LinkNode(int val) {
            this.val = val;
        }
    }
    public LinkNode head;//代表链表的头
    public LinkNode list;//代表链表的尾部
    @Override
    public void addFirst(int data) {
        LinkNode node=new LinkNode(data);
        if(head==null){
            head=node;
            list=node;
        }
        else {
            node.next=head;
            head.prev=node;
            head=node;
        }
    }

    @Override
    public void addLast(int data) {
        LinkNode node=new LinkNode(data);
        if(list==null){
            head=node;
            list=node;
        }
        else {
            list.next=node;
            node.prev=list;
            list=node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        if(index<0||index>size()){
            throw new LIstIndexoutofboundption("index下标越界了");
        }
        if(index==0){
            addFirst(data);
            return;//一定要加return
        }
        if(index==size()){
            addLast(data);

        }
        else {
            LinkNode node=new LinkNode(data);
            LinkNode cut=findindex(index);
            node.next=cut;
            cut.prev.next=node;
            node.prev=cut.prev;
            cut.prev=node;
        }

    }
    public LinkNode findindex(int index){
        if(index<0||index>size()){
            throw new RuntimeException("链表中没有index位置的数据");
        }
        LinkNode cut=head;
        while (index!=0){
            cut=cut.next;
            index--;
        }
        return cut;
    }
    @Override
    public boolean contains(int key) {
        LinkNode cut=list;
        while (cut!=null){
            if(cut.val==key){
                return true;
            }
            cut=cut.prev;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        LinkNode cut=head;
        while(cut!=null){
            if(cut.val==key){
                if (cut==head) {
                    head=head.next;
                    if(head==null){
                        list=null;
                        return;
                    }
                    head.prev=null;
                    return;

                } else if (cut.next == null) {
                    cut.prev.next = null;
                    list=null;
                    return;
                } else {
                    cut.prev.next = cut.next;
                    cut.next.prev = cut.prev;
                    return;
                }
            }
            else{
                cut=cut.next;
            }
        }
        if(cut==null){
            System.out.println("没有找到");
        }

    }

    @Override
    public void removeAllKey(int key) {
        LinkNode cur=head;
        while(cur!=null){
            LinkNode cut=cur;
            if(cut.val==key){
                if (cut==head) {
                    head=head.next;
                    if(head==null){
                        list=null;

                    }
                    head.prev=null;

                } else if (cut.next == null) {
                    cut.prev.next = null;
                    list=null;

                } else {
                    cut.prev.next = cut.next;
                    cut.next.prev = cut.prev;

                }
            }
                cur=cur.next;
        }
        if(cur==null){
            System.out.println("没有找到");
        }
    }

    @Override
    public int size() {
       LinkNode cut=head;
       int count=0;
       while (cut!=null){
           count++;
           cut=cut.next;
       }
       return count;
    }

    @Override
    public void clear() {
        LinkNode cut=head;
        while (cut!=null){
            LinkNode cutN=cut.next;
            cut.prev=null;
            cut.next=null;
        }

    }

    @Override
    public void display() {
        LinkNode cut=head;
        while (cut!=null){
            System.out.print(" "+cut.val);
            cut=cut.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        MyLinkedLIst myLinkedLIst=new MyLinkedLIst();
        myLinkedLIst.addLast(0);
        myLinkedLIst.addLast(0);
        myLinkedLIst.addLast(1);
        myLinkedLIst.addLast(2);
        myLinkedLIst.addLast(3);
        myLinkedLIst.addLast(5);
        myLinkedLIst.addIndex(1,0);

        myLinkedLIst.remove(5);
        myLinkedLIst.removeAllKey(0);

        myLinkedLIst.display();
        System.out.println(myLinkedLIst.contains(1));
        System.out.println(myLinkedLIst.size());
    }
}
